﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Threading;

namespace NCRE学生考试端V1._0.Resful
{
    public class Serialize
    {

        #region 序列化
        /// <summary>
        /// 将数据转换成json
        /// </summary>
        /// <param name="person">密码，用户名</param>
        /// <returns></returns>
        public string Serialize1(StudentInfo studentInfo)
        {
            //序列化对象
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(StudentInfo));
            MemoryStream ms = new MemoryStream();
            //将对象转换成json存储
            ser.WriteObject(ms, studentInfo);

            string jsonStr = Encoding.UTF8.GetString(ms.ToArray());
            return jsonStr;
        }
        #endregion

        #region 反序列化
        /// <summary>
        /// 将获取json转为字符串
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public LoginEntity Deserialize(string jsonStr)
        {
            //反序列化对象
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(LoginEntity));
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(jsonStr);
            MemoryStream ms1 = new MemoryStream(byteData);
            LoginEntity flist = (LoginEntity)ser.ReadObject(ms1);
            return flist;
        }

        public UserData DeserializeSon(string jsonStr)
        {
            //反序列化对象
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(UserData));
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(jsonStr);
            MemoryStream ms1 = new MemoryStream(byteData);
            UserData flist = (UserData)ser.ReadObject(ms1);
            return flist;
        }
        #endregion
    }
}
